
var em_arena;
// 1 sequence for arenabtl 
// sol, arm, arm_c with x, y 
// result cb : 
CArenaBtl = function() {

this.class_arenabtl = 1;
//hnt = this;
// define stage 
this.action_stage = new Array(3);

this.prevDA_index = 0;

// store this ref 
em_arena = this;
};


CArenaBtl.DA_INVALID = 9;
CArenaBtl.SOL_INVALID = 10;
CArenaBtl.FAIL_ARMA_WCOST = 11;
CArenaBtl.NOT_SUFFICENT_F = 12;
CArenaBtl.NO_HERO = 13;
CArenaBtl.ATT_MAX = 14;


CArenaBtl.prototype = {

	doArenaBattle: function (battle_param) {
		// citinum, x, y , tr : {s:1} , success_cb, error_cb, notify_cb 
		this.param = battle_param;
		
		debug.log(this, retName(this), battle_param);
		// start 1st action 
		this.next_action_idx = 0; // reset index 
		this.goNextAction();
	},


	goNextAction: function() {
		// go next 
		this.next_action_idx += 1; 
		
		// done complete!
		if ( this.next_action_idx >= this.action_stage.length ) {
			debug.log(this, retName(this), ' Scan Action!! Complete ');
			this.param.success_cb(0);
			return;
		}
		
		
		debug.log(this, retName(this), this.action_stage[this.next_action_idx].cmd + ' start!! '
		,this.action_stage[this.next_action_idx].doaction
		,this);
	
	
	
		var outer = this;
		
		outer.action_stage[outer.next_action_idx].doaction.call(outer);
		
	},



doTest : function() {
this.goNextAction();
},

 




/*
	jsonp1321974261763({"code":0,"ret":{"hero":[
	{"id":29358,"gid":67,"g":16,"w":0,"uid":9286,"u":"\uac00\uc8fc","r":2,"gname":"\uace0\uad6c\ub824","gflag":11},
	{"id":51292,"gid":16,"g":16,"w":0,"uid":8984,"u":"sharon","r":2,"gname":"\uace0\uad6c\ub824","gflag":11},
	{"id":29041,"gid":59,"g":16,"w":0,"uid":5095,"u":"\ube5b","r":1,"gname":"\uba85\uc608","gflag":127},
	{"id":29683,"gid":87,"g":17,"w":2,"uid":14787,"u":"\uae30\uc2b5\ubf40","r":1,"gname":"\uace0\uad6c\ub824","gflag":11},
	{"id":34773,"gid":26,"g":17,"w":2,"uid":13165,"u":"zicl","r":2,"gname":"\ud3c9\ud654","gflag":104},
	{"id":59881,"gid":26,"g":17,"w":0,"uid":7151,"u":"alexpark","r":2,"gname":"\uc5d4\uc824","gflag":34},
	{"id":30287,"gid":46,"g":16,"w":0,"uid":12572,"u":"kalgus","r":2,"gname":"\uace0\uad6c\ub824","gflag":11},
	{"id":47245,"gid":121,"g":16,"w":6,"uid":2398,"u":"\uc2f8\uc774\ubb38","r":2,"gname":"","gflag":1},
	{"id":29978,"gid":88,"g":16,"w":5,"uid":5095,"u":"\ube5b","r":1,"gname":"\uba85\uc608","gflag":127},
	{"id":42950,"gid":10,"g":17,"w":0,"uid":5601,"u":"\ud5ec\uce74","r":2,"gname":"\uace0\uad6c\ub824","gflag":11}]}})
*/
checkArenaConscribe: function(data) {

			// find matini010
			var heroes = data.ret.hero;
			heroes.sort(function(a,b){
				return a.w - b.w;
			});
			var found = false;
			var outer = this;

			$.each( heroes, function(i, item){
				console.log('w:', item.w, ' gid : ' , item.id, 'lv', item.g);
				if (item.g > outer.lv ) {
					found = true;
					console.log('found lv' , item.g,' w:', item.w);
					outer.tgid = item.id;
					outer.t_w = item.w;
					return false;
				}
			});
			
			if ( found == false ||this.t_w > 4 ) {
				// retry ;
				this.param.success_cb(999);
				return;
			}
			
			// set timeout for emul hand arenabtl
			setTimeout( "em.arenabtl.goNextAction()" ,CUtil.RandTime(0));
			// em.arenabtl.goNextAction();
	},
	
	
	
	doArena_Conscribe: function(  ) {
		// this.lv = 10;
		// this.lv = $('#h_level').val();
		// debug.log ( this, ' h level',this.lv );
		//&city=14281&action=gen_list&extra=1
		var param = { lv : this.lv ,  cb : function (data) { 
				em.arenabtl.checkArenaConscribe.call(em.arenabtl, data); }
		};
		em.ahelper.arena_conscribe(param);
	},


//:36787749 
/*
	jsonp1321974261764({"code":0,"ret":{"exp":24631,"win":3}})
*/
checkArenaBattleConscribe: function(data) {

	var self = em.arenabtl;
		if ( data.code != 0) {
			this.param.error_cb('error!!!! result battle');
			
		} else {
			if ( ! self.h ) self.h = { e: 0, ex:0, te:0	}; 
			// ok
			this.param.notify_cb('result : exp :' + data.ret.exp  + ' , win:' + data.ret.win 
			 +  ' got E: ' + self.h.e  + "  " +
			self.h.ex +  '/' +  self.h.te + '  ' + 
			'\n set cur lv : ' +  this.lv);
			console.log('result : exp :' + data.ret.exp  + ' , win:' + data.ret.win);
			
			// set initial exp when win  
			if (data.ret.win > 1 &&  ! this.cur_exp ) this.cur_exp  = data.ret.exp;
			
			// win  and  exp  is low than before 
			if ( data.ret.win > 1 && this.cur_exp <  data.ret.exp ) {
				this.lv ++ ;
				this.cur_exp  =  data.ret.exp;	// clear : to re set new win exp 
			}
		}
			
			if ( (this.count ++ % 10) != 0 ) 	this.next_action_idx += 1; 
			// set timeout for emulhand arenabtl
			setTimeout( "em.arenabtl.goNextAction()" ,CUtil.RandTime(0));
			// em.arenabtl.goNextAction();
	},
	
	
	
	doArena_Battle_Conscribe: function(  ) {
		//&city=14281&action=gen_list&extra=1
		// var param = { gid : 55477, tgid: this.tgid,  cb : function (data) {  // tani  : main tani
			
			//  Hound :: gid  222  	39 id	96434
		var param = { gid : 97833 , tgid: this.tgid,  cb : function (data) { 	// greateload tani 
		// var param = { gid : 6487 , tgid: this.tgid,  cb : function (data) { 	//  pvp tani 
		// var param = { gid : 6235 , tgid: this.tgid,  cb : function (data) { 	// pvp cl
				em.arenabtl.checkArenaBattleConscribe.call(em.arenabtl, data); }
		};
		em.ahelper.arena_battle_conscribe(param);
	},






	checkConscribe: function(hero_arr) {
			// check bestH is exist  ..
			var self = em.arenabtl;
			self.h_gid = 4;
			debug.log( this,hero_arr );
			var matchedH = hero_arr.filter( function(el){ return el.gid == self.h_gid})[0];
			if ( ! matchedH) {
				// no same hero !!!  something Wrong!!!
				debug.log(this,3, 'no same hero !!!  something Wrong!!!');
				alert('error');
				return;
			} 		

			self.h = matchedH;
			// self.h.gid = matchedH.gid;
			// self.h.g = matchedH.g;
			self.lv = self.h.g;
		
		debug.log(this, '\n\n ~~~~~~~~~~~~~~~~~~~~   my tini lv  : ' , self.h.g ,   ' E: ', self.h.e ,
			self.h.ex, '/',  self.h.te,   
			'\n set cur lv : ' , this.lv,  self.h );
			// set timeout for emul hand this
			// setTimeout( "this.goNextAction()" ,CUtil.RandTime(1500));
			var outer = em.arenabtl;
			setTimeout( function() { outer.goNextAction(); } ,CUtil.RandTime(0));			
			// this.goNextAction();
	},
	
	
	
	doConscribe: function(  ) {
		//&city=14281&action=gen_list&extra=1
		var outer = this;
		var param = { citnum: 22289,  cb : function (data) { 
				em.arenabtl.checkConscribe.call(em.arenabtl, data); }
		};
		em.ahelper.conscribe(param);
	},




	initialize: function() {
		debug_log( " arenabtl init " );
		
		var outer = this;
		
		//set init lv 
		this.lv = $('#h_level').val();
		debug.log ( this, ' h level',this.lv );
		this.count = 0;
		
		// define actions : outer 占쎌쥜��
		this.action_stage = [
			{cmd: "test" , doaction: outer.doTest },
			{cmd: "conscribe_arena" , doaction: outer.doArena_Conscribe},
			{cmd: "doBAttale" , doaction: outer.doArena_Battle_Conscribe},
			{cmd: "doConscribe" , doaction: outer.doConscribe},
		
		];
		this.cur_action = ""; // empty ... reset 
		this.next_action_idx = 0;
	},

 

};

 


 